package com.xxl.job.executor.core.config.querydb;

import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;

import java.util.HashMap;
import java.util.Map;

/**
 * <h3>xxl-job</h3>
 * <p>动态数据源</p>
 *
 * @author : 来朋
 * @date : 2020-12-28 14:40
 **/
public class Ds2DynamicDataSource extends AbstractRoutingDataSource {

    private static Ds2DynamicDataSource instance;
    private static byte[] lock=new byte[0];
    private static Map<Object,Object> dataSourceMap=new HashMap<Object, Object>();

    @Override
    public void setTargetDataSources(Map<Object, Object> targetDataSources) {
        super.setTargetDataSources(targetDataSources);
        dataSourceMap.putAll(targetDataSources);
        super.afterPropertiesSet();// 必须添加该句，否则新添加数据源无法识别到
    }

    public Map<Object, Object> getDataSourceMap() {
        return dataSourceMap;
    }

    @Override
    protected Object determineCurrentLookupKey() {
        String dbKey = Ds2DatabaseContextHolder.getDBKey();
		/*if (StringUtils.isBlank(dbKey)) {
			dbKey = "read";
		}*/
        return dbKey;
    }

    private Ds2DynamicDataSource() {}

    public static synchronized Ds2DynamicDataSource getInstance(){
        if(instance==null){
            synchronized (lock){
                if(instance==null){
                    instance=new Ds2DynamicDataSource();
                }
            }
        }
        return instance;
    }
}
